package com.hjrpc.babyyamall.product.service.impl;

import com.hjrpc.babyyamall.product.entity.SkuImagesEntity;
import com.hjrpc.babyyamall.product.entity.SpuInfoDescEntity;
import com.hjrpc.babyyamall.product.service.*;
import com.hjrpc.babyyamall.product.vo.SkuItemVo;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Map;
import java.util.concurrent.CompletableFuture;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.ThreadPoolExecutor;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.hjrpc.common.utils.PageUtils;
import com.hjrpc.common.utils.Query;

import com.hjrpc.babyyamall.product.dao.SkuInfoDao;
import com.hjrpc.babyyamall.product.entity.SkuInfoEntity;


@Service("skuInfoService")
public class SkuInfoServiceImpl extends ServiceImpl<SkuInfoDao, SkuInfoEntity> implements SkuInfoService {

    @Autowired
    SkuImagesService skuImagesService;

    @Autowired
    SpuInfoDescService spuInfoDescService;

    @Autowired
    AttrGroupService attrGroupService;

    @Autowired
    AttrService attrService;

    @Autowired
    ThreadPoolExecutor executor;

    @Override
    public PageUtils queryPage(Map<String, Object> params) {
        IPage<SkuInfoEntity> page = this.page(
                new Query<SkuInfoEntity>().getPage(params),
                new QueryWrapper<SkuInfoEntity>()
        );

        return new PageUtils(page);
    }

    @Override
    public PageUtils queryPageWithCondition(Map<String, Object> params) {
        QueryWrapper<SkuInfoEntity> wrapper = new QueryWrapper<>();
        String key = (String) params.get("key");
        if (StringUtils.isNotBlank(key)) {
            wrapper.and(x -> x.eq("sku_id", key).or().like("sku_name", key));
        }

        String catelogId = (String) params.get("catelogId");
        if (StringUtils.isNotBlank(catelogId) && Long.parseLong(catelogId) != 0) {
            wrapper.eq("catalog_id", catelogId);
        }
        //brandId: 0
        String brandId = (String) params.get("brandId");
        if (StringUtils.isNotBlank(brandId) && Long.parseLong(brandId) != 0) {
            wrapper.eq("brand_id", brandId);
        }
        String min = (String) params.get("min");
        String max = (String) params.get("max");
        if (StringUtils.isNotBlank(min) && Long.parseLong(min) != 0) {
            wrapper.ge("price", min);
        }
        if (StringUtils.isNotBlank(max) && Long.parseLong(max) != 0) {
            wrapper.le("price", max);
        }
        IPage<SkuInfoEntity> page = this.page(
                new Query<SkuInfoEntity>().getPage(params),
                wrapper
        );

        return new PageUtils(page);
    }

    @Override
    public SkuItemVo getSkuItem(Long skuId) {
        SkuItemVo skuItemVo = new SkuItemVo();

        CompletableFuture<SkuInfoEntity> skuInfoFuture = CompletableFuture.supplyAsync(() -> {
            // sku基本信息
            SkuInfoEntity skuInfo = this.getById(skuId);
            skuItemVo.setSkuInfo(skuInfo);
            return skuInfo;
        },executor);

        CompletableFuture<Void> skuImagesFuture = CompletableFuture.runAsync(() -> {
            // sku的图片信息
            List<SkuImagesEntity> skuImages = skuImagesService.lambdaQuery().eq(SkuImagesEntity::getSkuId, skuId).list();
            skuItemVo.setImages(skuImages);
        }, executor);

        CompletableFuture<Void> spuInfoDescFuture = skuInfoFuture.thenAcceptAsync(skuInfo -> {
            // spu的描述信息
            SpuInfoDescEntity spuInfoDesc = spuInfoDescService.lambdaQuery().eq(SpuInfoDescEntity::getSpuId, skuInfo.getSpuId()).one();
            skuItemVo.setSpuInfoDesc(spuInfoDesc);
        }, executor);

        CompletableFuture<Void> itemAttrGroupsFuture = skuInfoFuture.thenAcceptAsync(skuInfo -> {
            // spu的分组信息
            List<SkuItemVo.SpuItemAttrGroup> itemAttrGroups =  attrGroupService.querySpuGroupDetail(skuInfo.getSpuId(),skuInfo.getCatalogId());
            skuItemVo.setGroups(itemAttrGroups);
        }, executor);

        CompletableFuture<Void> skuItemSaleAttrsFuture = skuInfoFuture.thenAcceptAsync(skuInfo -> {
            // sku的销售属性
            List<SkuItemVo.SpuItemSaleAttr> skuItemSaleAttrs = attrService.querySkuSaleInfo(skuInfo.getSpuId());
            skuItemVo.setSaleAttrs(skuItemSaleAttrs);
        }, executor);

        try {
            CompletableFuture.allOf(skuImagesFuture,spuInfoDescFuture,itemAttrGroupsFuture,skuItemSaleAttrsFuture).get();
        } catch (Exception e) {
            e.printStackTrace();
            throw new RuntimeException(e.getMessage());
        }

        return skuItemVo;
    }

}